<?php


namespace App\Model;


use EasySwoole\ORM\DbManager;

class TransportModel extends \EasySwoole\ORM\AbstractModel
{
    protected $connectionName = 'default';
    protected $tableName = "psw_waybill_list";

    //获取顶部amount数
    public function getWaybillTitle($field = ["*"])
    {
        return DbManager::getInstance()->invoke(function ($client) use ($field){

         //查询amount['total']
         $t = self::invoke($client);
         $t->field($field)->withTotalCount()->all();
         $amount['total'] = $t->lastQueryResult()->getTotalCount();

         //已销单
         $d = self::invoke($client);
         $d->field($field)->where('is_destroy','1','=')->withTotalCount()->all();
         $amount['destroy'] = $d->lastQueryResult()->getTotalCount();
            //打印data的SQL语句
            print_r("打印destroy最后的SQL语句:" . $d->lastQuery()->getLastQuery());
            echo PHP_EOL;

         //未销单
         $u = self::invoke($client);
         $u->field($field);
         $u->where('is_destroy','3','=')->withTotalCount()->all();
         $amount['undestroy'] = $u->lastQueryResult()->getTotalCount();

         //审核中
         $c = self::invoke($client);
         $where['is_destroy'] = ['4','='];
         $c->field($field)->where($where)->withTotalCount()->all();
         $amount['check'] = $c->lastQueryResult()->getTotalCount();

         //驳回
         $r = self::invoke($client);
         $r->field($field)->where('is_destroy','5','=')->withTotalCount()->all();
         $amount['reject'] = $r->lastQueryResult()->getTotalCount();

//         print_r("打印顶部amount最后的SQL语句:" . $t->lastQuery()->getLastQuery());
//         echo PHP_EOL;
         return $amount;

        });
    }
    //获取运单数据
    public function getWaybillList($par,$field = ["*"]){
        return DbManager::getInstance()->invoke(function ($client) use ($par,$field){
            $m = self::invoke($client);
            $m->alias('a');
            $m->join('psw_pounds_list as b','a.order_id = b.associated_waybill');
            $m->field($field);

            $where = [];

            //amount筛选
            if ($par['tab'] != ''){
                if ($par['tab'] == '2') {
                    $where['is_destroy'] = ['1','='];
                } elseif ($par['tab'] == '3') {
                    $where['is_destroy'] = ['2','='];
                } elseif ($par['tab'] == '4') {
                    $where['is_destroy'] = ['3','='];
                } elseif ($par['tab'] == '5') {
                    $where['is_destroy'] = ['4','='];
                }
            }
            //运单号模糊查询
            if ($par['waybill_number'] && $par['waybill_number'] != ''){
                $where['order_id'] = ["%{$par['waybill_number']}%",'like'];
            }
            //运单状态筛选
            var_dump($par['waybill_status_ids']);
//            /////////////////////////
//            $where['status'] = ['0', '='];
//            if ($par['waybill_status_ids'] == '-1') {
//                $where['status'] = [[0,1,2,3,4], 'in'];
//            }
//            $par['waybill_status_ids'] &&
//            //////////////////////////////
            if ($par['waybill_status_ids'] != '-1') {
                var_dump($par['waybill_status_ids']);
                $array = explode(',',$par['waybill_status_ids']);
                $arr = [];
                foreach ($array as $k => $v) {
                    $arr[] = intval($v);
                }
                $where['status'] = [$arr,'in'];
            }
            //运单类型筛选
            if ($par['waybill_type_ids'] && $par['waybill_type_ids'] != '-1') {
                if ($par['waybill_type_ids'] == 'abnormal'){
                    $where['waybill_type_ids'] = [[2,3,4],'in'];
                }else{
                    $array = explode(',',$par['waybill_type_ids']);
                    $waybill_type_ids = [];
                    foreach ($array as $k => $v) {
                        $waybill_type_ids[] = intval($v);
                    }
//                    var_dump($waybill_type_ids);
                    $where['waybill_type_ids'] = [$waybill_type_ids,'in'];
                }
            }
            //起点模糊检索
            if($par['start_point']) {
                $where['start_address'] = ["%{$par['start_point']}%", 'like'];
            }
            //终点模糊检索
            if($par['end_point']) {
                $where['end_address'] = ["%{$par['end_point']}%", 'like'];
            }
            //开始时间检索
            if($par['start_time1']&& $par['end_time1']) {
                $where['start_time'] = [[$par['start_time1'],$par['end_time1']],'between'];
            }
            //结束时间检索
            if($par['start_time2'] && $par['start_time2'] != '' && $par['end_time2'] && $par['end_time2'] != '') {
                $where['end_time'] = [[$par['start_time2'],$par['end_time2']],'between'];
            }
            //土质类型
            if ($par['soil_type_id']&& $par['soil_type_id'] != '-1') {
                $array = explode(',',$par['soil_type_id']);
                var_dump($array);
                $soil_type_name = [];
                foreach ($array as $k => $v) {
                    $soil_type_name[] = intval($v);
                }
                    var_dump($soil_type_name);
                $where['soil_type_name'] = [$soil_type_name,'in'];
            }
            //车牌号模糊查询
            if ($par['vehicle_number']){
                $where['a.license_plate'] = ["%{$par['vehicle_number']}%",'like'];
            }
            //司机模糊查询
            if ($par['driver']) {
                $where['driver_name'] = ["%{$par['driver']}%",'like'];
            }
            //关联磅单查询
            if ($par['belong_pound'] && $par['belong_pound'] !== '-1') {
                if ($par['belong_pound'] ==  '2') {
                    $where['b.suttle'] = ["null",'='];
                }
                if ($par['belong_pound'] ==  '1') {
                    $where['b.suttle'] = ["null",'!='];
                }
            }
            if ($where){
                $m->where($where);
            }

            $m->withTotalCount();

            //分页限制
            $m->limit((($par['pn'] - 1) * $par['page_size']),$par['page_size']);

            //data排序
            //设置默认排序规则
            $sort_key = 'enter_weighbridge';
            $sort_type = 'desc';

            //当有参数传递时的排序规则
            if ($par['sort_key']){
                 $sort_key = $par['sort_key'];
            }

            if ($par['sort_type']) {
                if ($par['sort_type'] == 'ascend'){
                    $sort_type = 'asc';
                }else{
                    $sort_type = 'desc';
                }
            }

            $m->order($sort_key,$sort_type);
            $m->all();


            //打印data的SQL语句
            print_r("打印data最后的SQL语句:" . $m->lastQuery()->getLastQuery());
            echo PHP_EOL;

            $getWaybillList['data'] =  $m->lastQueryResult()->getResult();
            $getWaybillList['total'] =  $m->lastQueryResult()->getTotalCount();

//            echo '得到数据数' . print_r($getWaybillList['total']);
//            echo PHP_EOL;
            return $getWaybillList;
        });
    }
    //获取运单详细信息
    public function getWaybillDetail($par,$field){
        return DbManager::getInstance()->invoke(function ($client) use ($par,$field){
            $m = self::invoke($client);
            $m->field($field);
            $m->alias('a');
            $m->join('psw_order_info as c','a.order_id = c.waybill_id');
            $m->where('a.id',$par['id'],'=');
            $m->get();

//            //打印“getWaybillDetail”的Sql语句
//            print_r("打印“getWaybillDetail”的Sql语句:" . $m->lastQuery()->getLastQuery());
//            print PHP_EOL;
//            echo "SQL语句正确";
//            echo PHP_EOL;

            $getWaybillDetail = $m->lastQueryResult()->getResult();
//            print_r($getWaybillDetail[0]);
            return $getWaybillDetail;
        });
    }
    //获取获取异常信息
    public function getOverTimeInfo($par,$getWaybillDetail){
        return DbManager::getInstance()->invoke(function ($client) use ($par,$getWaybillDetail){

            var_dump($getWaybillDetail);

            //数值偏差
            $driver_write_weight = $getWaybillDetail[0]['enter_weighbridge'];

            $offset = null;
            if ($driver_write_weight < 5000) {
                $reference_weight = "5000";
                $offset = abs($reference_weight - $driver_write_weight);

                echo "输出重量低于5000的偏差值：";
                print_r($offset);
                $offset = strval($offset) . "kg";
            }
            if ($driver_write_weight > 60000) {
                $reference_weight = "60000";
                $offset = abs($reference_weight - $driver_write_weight);

                echo "输出重量高于60000的偏差值：";
                print_r($offset);
                $offset = strval($offset) . "kg";
            }
            $driver_write_weight = strval($driver_write_weight) . "kg";
            $reference_weight = strval($reference_weight) . "kg";

            //超时
            $estimate_arrival_time = $getWaybillDetail[0]['estimated_arrival_time'];
            $end_time = $getWaybillDetail[0]['end_time'];
            $over_time = null;
            if (strtotime($end_time) - strtotime($estimate_arrival_time) > 0){
                $over_time = $this->secondsToTime(strtotime($end_time) - strtotime($estimate_arrival_time));
            }

            $result = [
                'offset' => $offset,
                'reference_weight' => $reference_weight,
                'driver_write_weight' => $driver_write_weight,
                'estimated_arrival_time' => $estimate_arrival_time,
                'end_time' => $end_time,
                'over_time' => $over_time
            ];

            return $result;
        });
    }
    //销单
    public function destroyWaybill($par,$destroy){
        return DbManager::getInstance()->invoke(function ($client) use ($par,$destroy){
            $d = self::invoke($client);
            $array = explode(',',$par['id']);
            $arr = [];
            foreach ($array as $k => $v) {
                $arr[] = intval($v);
            }
//            var_dump($arr);
            $where['id'] = [$arr,'in'];
            $d->where($where);
            $d->update(['is_destroy' => '1']);
//            print_r("打印最后的SQL语句:" . $d->lastQuery()->getLastQuery());
//            echo PHP_EOL;
            return $d->lastQueryResult()->getAffectedRows();
        });
    }


    //秒转时间
    public function secondsToTime($init) {
        $hours = floor($init / 3600);
        $minutes = floor(($init / 60) % 60);
        $seconds = $init % 60;
        return "{$hours}小时{$minutes}分{$seconds}秒";
    }
    //分割字符串
    public function poundArray($par) {
        $explode = explode(',',$par);
        $list = [];
        foreach ($explode as $k => $v) {
            $list[] = $v;
        }
        return $list;
    }
}